package com.yao.springcachedemo.config;

import com.yao.springcachedemo.entity.User;
import org.springframework.cache.CacheManager;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;

/**
 * ehCache的配置类
 *
 * @author yao
 */
@Configuration
public class EhCacheConfig {
    /**
     * 创建ehCacheManager，从3.0之后spring使用jCache操作ehcache
     *
     * @return
     */
    @Bean("ehCacheManager")
    public CacheManager ehCacheManager() {
        CachingProvider cachingProvider = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider");
        javax.cache.CacheManager cacheManager = cachingProvider.getCacheManager();
        cacheManager.createCache("user", mutableConfiguration());
        return new JCacheCacheManager(cacheManager);
    }


    public MutableConfiguration<String, User> mutableConfiguration() {
        MutableConfiguration<String, User> configuration = new MutableConfiguration<String, User>();
        configuration.setTypes(String.class, User.class);
        return configuration;
    }
}
